package com.baolai.config;

import com.alibaba.fastjson.JSONObject;
import com.baolai.entity.GoodsUser;
import com.baolai.service.GoodsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
class LoginInterceptor implements HandlerInterceptor {

    @Autowired
    GoodsUserService goodsUserService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Object userSession = session.getAttribute("user");
        if(userSession == null){
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            JSONObject json = new JSONObject();
            json.put("msg","/login");
            json.put("code",4);
            response.getWriter().append(json.toJSONString());
            return false;
        }
        session.setMaxInactiveInterval(3600);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
